﻿''' <summary>
''' Contains the functions related to methods and functions.
''' </summary>
''' <remarks></remarks>
Module Relations

    ''' <summary>
    ''' The address of the class containing the method to process
    ''' </summary>
    ''' <remarks></remarks>
    Public ClassAdd As String = ""

    ''' <summary>
    ''' Contians if to make a variable for the method or not.
    ''' </summary>
    ''' <remarks></remarks>
    Public MakeVar As Boolean = False

    ''' <summary>
    ''' The method being invoked.
    ''' </summary>
    ''' <remarks></remarks>
    Public CurrentMethod As String = ""

    ''' <summary>
    ''' Accepts the input of the invovation of the method.
    ''' </summary>
    ''' <param name="Request">Yje request of the method.</param>
    ''' <remarks></remarks>
    Sub Input(ByVal Request As String)

        Dim words As String = Request
        Dim split As String() = words.Split(New [Char]() {"("c, ")"c})

        Dim Name As String = split(0)
        Dim Params As String = split(1)

        FindMethod(Name, Params)
    End Sub

    ''' <summary>
    ''' Finds the method required/
    ''' </summary>
    ''' <param name="FullName">The name of the method to find.</param>
    ''' <param name="params">The parameters for the method to invoke.</param>
    ''' <remarks></remarks>
    Sub FindMethod(ByVal FullName As String, ByVal params As String)
        Dim words As String = FullName
        Dim split As String() = words.Split(New [Char]() {"."c})

        If split.Length = 2 Then

            Dim classname As String = split(0)
            Dim methodname As String = split(1)

            FindInclude(classname, methodname, params)

        ElseIf split.Length = 3 Then

        End If
    End Sub

    ''' <summary>
    ''' Finds the method from the include statements.
    ''' </summary>
    ''' <param name="ClassName">The name of the class containing the method to invoke.</param>
    ''' <param name="MethodName">The name of the method.</param>
    ''' <param name="params">The parameters for the method to invoke.</param>
    ''' <remarks></remarks>
    Sub FindInclude(ByVal ClassName As String, ByVal MethodName As String, ByVal params As String)
        Dim Solution As XElement = XElement.Load(Module1.SolAdd)
        Dim Includes As XElement = <Root>
                                       <%= From el In Solution.<Include> _
                                           Select el %>
                                   </Root>
        Dim Include As String = Includes

        Dim words2 As String = Include
        Dim split2 As String() = words2.Split(New [Char]() {CChar(vbLf)})

        Dim output As String = ""
        Dim s As String

        For Each s In split2
            output = output & s
        Next

        Dim words As String = output
        Dim split As String() = words.Split(New [Char]() {";"c})

        Dim ss As String
        For Each ss In split

            ss = ss.Trim()

            If ss Like "Include*" Then
                Dim includestmt As String = ss
                Dim includesplit As String() = includestmt.Split(New [Char]() {" "c})

                Dim add As String = includesplit(1)
                Dim addsplit As String() = add.Split(New [Char]() {"\"c})
                Dim addlength As Integer = addsplit.Length() - 1

                If addsplit(addlength) = ClassName & ".xml" Then
                    ClassAdd = add
                    LoadMethod(MethodName, params)
                End If
            End If
        Next
    End Sub

    ''' <summary>
    ''' Loads the method required.
    ''' </summary>
    ''' <param name="MethodName">The name od the method.</param>
    ''' <param name="params">The parameters for the method to invoke.</param>
    ''' <remarks></remarks>
    Sub LoadMethod(ByVal MethodName As String, ByVal params As String)
        Dim ClassFile As XElement = XElement.Load(ClassAdd)

        Dim code As String = GetCode(MethodName, ClassFile)
        ClassAdd = ""

        Dim words As String = code
        Dim split As String() = words.Split(New [Char]() {"{"c, "}"c})

        Dim metdec As String = split(0)

        Dim words2 As String = metdec
        Dim split2 As String() = words2.Split(New [Char]() {"("c, ")"c})

        Dim namesplit As String() = split2(0).Split(New [Char]() {" "c})
        CurrentMethod = namesplit(1)

        If MakeVar = True Then

            Dim VarDecl As String = split2(0)
            Basic.NewVar(VarDecl)

        End If

        Dim Parameters As String = split2(1)

        If Parameters <> "" And params <> "" Then
            Dim paramsplit As String() = Parameters.Split(New [Char]() {";"c})
            Dim s As String

            Dim length As Integer = paramsplit.Length - 1

            Dim paramsplit2 As String() = params.Split(New [Char]() {","c})

            For Each s In paramsplit
                s = s.Trim()
                If s = "" Or s = " " Then

                Else
                    Basic.NewVar(s)
                End If
            Next

            Dim i As Integer = -1

            Do Until i = length
                i = i + 1
                If paramsplit(i) = "" Or paramsplit(i) = " " Then

                Else
                    paramsplit(i) = paramsplit(i).Trim()
                    Dim varsplit As String() = paramsplit(i).Split(New [Char]() {" "c})
                    varsplit(1) = varsplit(1).Trim()
                    paramsplit2(i) = paramsplit2(i).Trim()

                    Dim stmt As String = varsplit(1) & " = " & paramsplit2(i)
                    Process(stmt)
                End If
            Loop

        End If

        Module1.Run(code)
    End Sub

    ''' <summary>
    ''' Gets the code from the method to invoke.
    ''' </summary>
    ''' <param name="Methodname">The name of the method.</param>
    ''' <param name="ClassFile">The class file to get the code from.</param>
    ''' <returns>The code from the method.</returns>
    ''' <remarks></remarks>
    Function GetCode(ByVal Methodname As String, ByVal ClassFile As XElement) As String
        Dim Code As String

        Dim MethodSearch As XElement = <root>
                                           <%= From el In ClassFile.<Method>.<Name> _
                                               Where el.Value = Methodname _
                                               Select el.Attribute("id").Value %>
                                       </root>
        Dim elid As Integer = MethodSearch

        Dim CodeSearch As XElement = <root>
                                         <%= From el In ClassFile.<Method>.<Code> _
                                             Where el.Attribute("id") = elid.ToString _
                                             Select el %>
                                     </root>
        Code = CodeSearch

        Return Code
    End Function

    ''' <summary>
    ''' Processes the return statement in a function.
    ''' </summary>
    ''' <param name="stmt"></param>
    ''' <remarks></remarks>
    Sub RetVal(ByVal stmt As String)
        Dim words As String = stmt
        Dim split As String() = words.Split(New [Char]() {" "c})

        Dim output As String
        output = CurrentMethod & " = " & split(1)

        Basic.AssignVal(output)
    End Sub
End Module
